package com;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.Format;
import java.util.HashMap;

import javax.imageio.ImageIO;

import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class ReadQRCode {
	
	public static void main(String[] args) throws IOException {
		
		MultiFormatReader formatReader=new MultiFormatReader();
		
		//路径
		File file=new File("img.png");
		BufferedImage image = ImageIO.read(file);
		
		LuminanceSource luminanceSource=new BufferedImageLuminanceSource(image);
		Binarizer binarizer= new HybridBinarizer(luminanceSource);
		
		//设置格式
		HashMap hints=new HashMap();
		hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
		hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);
		hints.put(EncodeHintType.MARGIN,2);
		
		BinaryBitmap binaryBitmap=new BinaryBitmap(binarizer);
		
		
		try {
			Result result=formatReader.decode(binaryBitmap, hints);
			
			System.out.println("解析结果："+result.toString());
			System.out.println("解析结果："+result.getBarcodeFormat());
			System.out.println("解析结果："+result.getText());
		} catch (NotFoundException e) {
			e.printStackTrace();
		}
	}

}
